<!-- AdminLogin.vue -->
<template>
  <div class="admin-login-container">
    <div class="background-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>

    <el-card class="login-card">
      <div class="card-header-wrapper">
        <div class="admin-icon">
          <svg viewBox="0 0 24 24" width="48" height="48">
            <path fill="currentColor"
              d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
          </svg>
        </div>
        <h2 class="card-title">管理员登录</h2>
        <p class="card-subtitle">请输入管理员账号和密码</p>
      </div>

      <el-form ref="formRef" :model="form" :rules="rules" label-width="0" class="login-form">
        <div class="form-group">
          <div class="input-wrapper">
            <span class="input-icon">
              <el-icon>
                <User />
              </el-icon>
            </span>
            <el-form-item prop="email" class="no-margin">
              <el-input v-model="form.email" placeholder="管理员账号" size="large" class="custom-input" />
            </el-form-item>
          </div>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <span class="input-icon">
              <el-icon>
                <Lock />
              </el-icon>
            </span>
            <el-form-item prop="password" class="no-margin">
              <el-input v-model="form.password" type="password" placeholder="登录密码" size="large" show-password
                class="custom-input" @keyup.enter="handleSubmit" />
            </el-form-item>
          </div>
        </div>

        <div class="form-group">
          <div class="input-wrapper">
            <span class="input-icon">
              <el-icon>
                <Key />
              </el-icon>
            </span>
            <el-form-item prop="captcha" class="no-margin">
              <el-input v-model="form.captcha" placeholder="图形验证码" size="large" class="custom-input captcha-input"
                @keyup.enter="handleSubmit" />
            </el-form-item>
            <div class="captcha-wrapper" @click="refreshCaptcha">
              <div v-if="captchaSvg" class="captcha-image" v-html="captchaSvg"></div>
              <div v-else class="captcha-placeholder">点击刷新验证码</div>
            </div>
          </div>
        </div>

        <el-form-item class="submit-item">
          <el-button type="primary" :loading="loading" @click="handleSubmit" class="submit-btn" size="large">
            <span v-if="!loading">进入管理系统</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>
      </el-form>
      <div class="card-footer">
        <el-button type="text" @click="switchToUserLogin" class="switch-btn">
          <span class="switch-icon">←</span>
          返回用户登录
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { sha1 } from 'js-sha1'
import { useAdminStore } from '@/stores/admin'

const router = useRouter()
const adminStore = useAdminStore()

const loading = ref(false)
const formRef = ref()
const captchaSvg = ref('')
const captchaId = ref('')

// 表单数据
const form = reactive({
  email: '',
  password: '',
  captcha: ''
})

// 验证规则
const rules = {
  email: [
    { required: true, message: '请输入管理员账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 4, max: 4, message: '验证码长度为4个字符', trigger: 'blur' }
  ]
}

// 获取验证码
const refreshCaptcha = async () => {
  try {
    const response = await adminStore.getCaptchaAction()
    if (response.code === 0) {
      captchaSvg.value = response.data.captcha
      captchaId.value = response.data.captchaId
    }
  } catch (error) {
    ElMessage.error(error.message || '获取验证码失败')
  }
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()

    if (!captchaId.value) {
      ElMessage.warning('请先获取验证码')
      await refreshCaptcha()
      return
    }

    loading.value = true

    // 管理员登录
    const response = await adminStore.adminLogin({
      email: form.email,
      password: form.password,
      captcha: form.captcha,
      captchaId: captchaId.value
    })

    ElMessage.success(response.msg || '管理员登录成功')

    // 登录成功后跳转到管理后台
    router.push('/admin/dashboard')

  } catch (error) {
    ElMessage.error(error.message || '管理员登录失败')
    // 登录失败时刷新验证码
    await refreshCaptcha()
    form.captcha = '' // 清空验证码输入框
  } finally {
    loading.value = false
  }
}

// 切换到用户登录
const switchToUserLogin = () => {
  router.push('/login')
}

// 组件挂载时获取验证码
onMounted(() => {
  refreshCaptcha()
})
</script>

<style scoped>
.captcha-input {
  flex: 1;
}

.captcha-wrapper {
  margin-left: 12px;
  width: 120px;
  height: 40px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: #f8fafc;
  transition: all 0.3s ease;
}

.captcha-wrapper:hover {
  border-color: #667eea;
  background: #f0f4ff;
}

.captcha-image {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.captcha-placeholder {
  color: #a0aec0;
  font-size: 12px;
  text-align: center;
}

/* 调整输入框容器布局 */
.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.admin-login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: -50px;
  right: -50px;
}

.circle-2 {
  width: 150px;
  height: 150px;
  bottom: 100px;
  left: -50px;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: -30px;
  right: 20%;
}

.login-card {
  width: 420px;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: none;
  position: relative;
  z-index: 1;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
}

.card-header-wrapper {
  text-align: center;
  padding: 30px 0 20px;
}

.admin-icon {
  color: #667eea;
  margin-bottom: 16px;
}

.card-title {
  margin: 0 0 8px 0;
  color: #2d3748;
  font-size: 24px;
  font-weight: 600;
}

.card-subtitle {
  margin: 0;
  color: #718096;
  font-size: 14px;
}

.login-form {
  padding: 0 30px;
}

.form-group {
  margin-bottom: 24px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 12px;
  z-index: 2;
  color: #a0aec0;
}

:deep(.custom-input .el-input__wrapper) {
  padding-left: 44px;
  border-radius: 10px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
  border: 1px solid #e2e8f0;
  transition: all 0.3s ease;
}

:deep(.custom-input .el-input__wrapper:hover),
:deep(.custom-input .el-input__wrapper.is-focus) {
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
  border-color: #667eea;
}

.no-margin {
  margin-bottom: 0;
  width: 100%;
}

.submit-item {
  margin-bottom: 0;
}

.submit-btn {
  width: 100%;
  height: 48px;
  border-radius: 10px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  font-weight: 500;
  font-size: 16px;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

.card-footer {
  padding: 20px 30px 30px;
  text-align: center;
  border-top: 1px solid #f1f5f9;
}

.switch-btn {
  color: #718096;
  font-size: 14px;
  transition: color 0.3s ease;
}

.switch-btn:hover {
  color: #667eea;
}

.switch-icon {
  margin-right: 4px;
}
</style>